package org.fat.nred.actd.tenant.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>文档配置</p>
 *
 * @author TanXin
 * @since 2021/10/20 17:23
 */
@Configuration
@EnableSwagger2WebMvc
@ConditionalOnProperty(value = {"knife4j.enable"}, matchIfMissing = true)
public class SwaggerConfig {


    @Bean(value = "allGroupApi")
    public Docket appGroupApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("all")
                .select()
                .apis(RequestHandlerSelectors.basePackage("org.fat.nerd.actd.tenant.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(operationParameters());
    }


    private List<Parameter> operationParameters() {
        List<Parameter> parameters = new ArrayList<>();
        Parameter parameter = new ParameterBuilder()
                .name("Authorization")
                .description("用户认证token")
                .required(false)
                .modelRef(new ModelRef("String"))
                .parameterType("header")
                .build();
        parameters.add(parameter);
        return parameters;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("动态分库分表技术验证文档")
                .description("崃州项目文档-knife4j")
                .termsOfServiceUrl("https://www.xx.com/")
                .contact(new Contact("Tx", "https://fat.boot.org", "Nan"))
                .version("1.0")
                .build();
    }
}
